package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.AwardPackageDao;
import com.zhangtai.modules.entity.salary.AwardPackageEntity;
import com.zhangtai.modules.service.salary.AwardPackageService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
public class AwardPackageServiceImpl extends ServiceImpl<AwardPackageDao, AwardPackageEntity> implements AwardPackageService {

    @Override
    public PageResult queryPage(@RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                @RequestParam(value = "serialNum") String serialNum,
                                @RequestParam (value = "applicantName") String applicantName,
                                @RequestParam(value = "applyTimeStart") String applyTimeStart,
                                @RequestParam(value = "applyTimeEnd") String applyTimeEnd,
                                @RequestParam(value = "awardName") String awardName,
                                @RequestParam(value = "distributionStatus") ArrayList<Integer> distributionStatus,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<AwardPackageEntity> page = this.page(
                new Page<AwardPackageEntity>(pageNum,pageSize),
                new QueryWrapper<AwardPackageEntity>()
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .like(!StringUtils.isEmpty(serialNum),"serial_num",serialNum)
                        .like(!StringUtils.isEmpty(applicantName),"applicant_name",applicantName)
                        .ge(!com.zhangtai.utils.StringUtils.isEmpty(applyTimeStart),"apply_time",applyTimeStart+" 00:00:00")
                        .le(!com.zhangtai.utils.StringUtils.isEmpty(applyTimeEnd),"apply_time",applyTimeEnd+" 29:59:59")
                        .like(!StringUtils.isEmpty(awardName),"award_name",awardName)
                        .in(!CollectionUtils.isEmpty(distributionStatus),"status",distributionStatus)
                        .eq("is_delete",0).orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public AwardPackageEntity getByIdE(Long id) {
            AwardPackageEntity one = this.getOne(new QueryWrapper<AwardPackageEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(AwardPackageEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(AwardPackageEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            AwardPackageEntity entity = new AwardPackageEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}